#include "ShaderStorageBuffer.h"

#ifdef _WIN32
using namespace Gut;

bool ShaderStorageBuffer::Create(const Desc &desc)
{
    glDeleteBuffers(1, &SSBO);

    glGenBuffers(1, &SSBO);
    glBindBuffer(GL_SHADER_STORAGE_BUFFER, SSBO);
    glBufferData(GL_SHADER_STORAGE_BUFFER, desc.dataLen, desc.data, desc.usage);
    glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, SSBO);
    glBindBufferRange(GL_SHADER_STORAGE_BUFFER, desc.binding, SSBO, 0, desc.dataLen);

    return true;
};

void ShaderStorageBuffer::Read(void *data, int dataLen)
{
    glBindBuffer(GL_SHADER_STORAGE_BUFFER, SSBO);
    void *pData = glMapBuffer(GL_SHADER_STORAGE_BUFFER, GL_READ_ONLY);
    memcpy(data, pData, dataLen);
    glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);
}

void ShaderStorageBuffer::Delete()
{
    glDeleteBuffers(1, &SSBO);
}

#endif